package com.ruoyi.quartz.controller;

import java.util.List;

import com.ruoyi.common.core.controller.BasicsController;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.page.ResponsePageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.quartz.util.CronUtils;

/**
 * 调度任务信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = {"定时任务模块"})
@RestController
@RequestMapping("/monitor/job")
public class SysJobController extends BasicsController
{
    @Autowired
    private ISysJobService jobService;

    /**
     * 查询定时任务列表
     */
    @ApiOperation("定时任务列表")
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @PostMapping("/list")
    public ResponsePageInfo<SysJob> list(@RequestBody SysJob sysJob)
    {
        startPage();
        List<SysJob> list = jobService.selectJobList(sysJob);
        return toResponsePageInfo(list);
    }
    /**
     * 获取定时任务详细信息
     */
    @ApiOperation("获取定时任务详细信息")
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping(value = "/getInfo")
    @ApiImplicitParam(name = "jobId",value = "定时ID" ,dataType = "String", paramType = "query", required = true)
    public Response<SysJob> getInfo(Long jobId)
    {
        SysJob sysJob = jobService.selectJobById(jobId);
        return Response.success(sysJob);
    }

    /**
     * 新增定时任务
     */
    @ApiOperation("新增定时任务")
    @PreAuthorize("@ss.hasPermi('monitor:job:add')")
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public Response add(@RequestBody SysJob sysJob) throws SchedulerException, TaskException
    {
        if (!CronUtils.isValid(sysJob.getCronExpression()))
        {
            return Response.fail("cron表达式不正确");
        }
        sysJob.setCreateBy(SecurityUtils.getUsername());
        jobService.insertJob(sysJob);
        return Response.success("添加定时任务成功");
    }

    /**
     * 修改定时任务
     */
    @ApiOperation("修改定时任务")
    @PreAuthorize("@ss.hasPermi('monitor:job:edit')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public Response edit(@RequestBody SysJob sysJob) throws SchedulerException, TaskException
    {
        if (!CronUtils.isValid(sysJob.getCronExpression()))
        {
            return Response.fail("cron表达式不正确");
        }
        sysJob.setUpdateBy(SecurityUtils.getUsername());
        jobService.updateJob(sysJob);
        return Response.success("编辑定时任务成功");
    }

    /**
     * 定时任务状态修改
     */
    @ApiOperation("定时任务状态修改")
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    public Response changeStatus(@RequestBody SysJob job) throws SchedulerException
    {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        jobService.changeStatus(newJob);
        return Response.success("定时任务状态修改成功");
    }

    /**
     * 定时任务立即执行一次
     */
    @ApiOperation("定时任务立即执行一次")
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PostMapping("/run")
    public Response run(@RequestBody SysJob job) throws SchedulerException
    {
        jobService.run(job);
        return Response.success();
    }

    /**
     * 删除定时任务
     */
    @ApiOperation("删除定时任务")
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @GetMapping("/remove")
    @ApiImplicitParam(name = "jobIds",value = "定时ID 数组" ,dataType = "long",required = true)
    public Response remove(Long[] jobIds) throws SchedulerException, TaskException
    {
        jobService.deleteJobByIds(jobIds);
        return Response.success();
    }

    /**
     * 校验cron表达式是否有效
     */
    @ApiOperation("校验cron表达式是否有效")
    @PostMapping("/checkCronExpressionIsValid")
    @ResponseBody
    public boolean checkCronExpressionIsValid(@RequestBody  SysJob job)
    {
        return jobService.checkCronExpressionIsValid(job.getCronExpression());
    }
}
